package Game.GameElements
{
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2ContactListener;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.Contacts.b2Contact;
	import Common.Utils;

	import flash.display.DisplayObject;	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;

	import Common.ContactListener;
	import Common.Managers.SwfManager;
	
	import Game.Effects.Explosion;
	import Game.GameStates.GameState;
	import Game.World;
	/**
	 * ...
	 * @author 
	 */
	public class Rocket extends GameObject
	{
		public static const DIRECTION_UP:int 		= 1;
		public static const DIRECTION_RIGHT:int 	= 2;
		public static const DIRECTION_DOWN:int 		= 3;		
		public static const DIRECTION_LEFT:int 		= 4;		
		
		private var 		m_currentDirection:b2Vec2 	= null;
		private var 		m_currentSpeed:Number	= 30;
		public  var 		isExplosion:Boolean		= false;				
		// Tank which released rocket
		public  var 		m_owner:GameObject		= null;
		
		public  static var 	m_swfPath:String 		= Settings.pathRes + "swf/rocket.swf";
		private static var 	m_rocketType:Array 		= ["rocket", "rocket", "rocket", "rocket"];
		
		public function Rocket(startX:int, startY:int, angle:Number, owner:GameObject) 
		{			
			super(startX, startY, angle, m_swfPath, m_rocketType, 0, true, false);
			World.instance.objectLayer.addChild(this);
			m_owner = owner;
		
			gameObjects.push(this);
			
			m_currentDirection = Utils.getDirection(this);
		}
		
		override public function update():void
		{
			super.update();
			if (objectSprite == null) return;

			var force:b2Vec2 = Utils.getDirection(this);
			force.Multiply(m_currentSpeed);
			
			if (force.x || force.y) 
			{
				physicalObject.ApplyForce(force, physicalObject.GetWorldCenter());
			}
			
			if (isExplosion)
			{
				new Explosion(this.x, this.y);
			
				m_owner = null;
				World.instance.objectLayer.removeChild(this);
				
				release();
			}
		}
		
		// Create explosion
		override public function doDestroy():void
		{			
			isExplosion = true;
		}
	}
}
